using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TuyennvLibrary.SqlClient;
using ServerApi.Models;
namespace ServerApi.Models.Menu
{
    public class RoleUser
    {
        public int Id { get; set; }
        public int RoleId { get; set; }
        public int UserId { get; set; }


        public static string COLUMN_ID = "Id";
        public static string COLUMN_ROLEID = "RoleId";
        public static string COLUMN_USERID = "UserId";


        public static string TableName = "RoleUser";
        public RoleUser()
        {
            this.Id = 0;
            this.RoleId = 0;
            this.UserId = 0;

        }
    }
    public class RoleUserModel : BaseModel<RoleUser>
    {
        public RoleUserModel()
        {
            table = RoleUser.TableName;
        }
        public bool Insert(RoleUser obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter(RoleUser.COLUMN_ROLEID,obj.RoleId),
					new SqlParameter(RoleUser.COLUMN_USERID,obj.UserId),
                    
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Update(RoleUser obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter(RoleUser.COLUMN_ROLEID,obj.RoleId),
					new SqlParameter(RoleUser.COLUMN_USERID,obj.UserId),

                }, new SqlParameter[]{
					new SqlParameter(RoleUser.COLUMN_ID,obj.Id),
										 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Delete(int Id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter(RoleUser.COLUMN_ID,Id),
										 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<RoleUser> GetByPage(int page, int pagesize, List<RoleUser> source)
        {
            List<RoleUser> list = new List<RoleUser>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<RoleUser> GetListBy(DataTable dt)
        {
            List<RoleUser> list = new List<RoleUser>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    RoleUser obj = GetBy(dr);
                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetListAllFullField()
        {
            try
            {
                string query = "select a.*,b.Name as rolename, d.Username,c.Name as typerole from " + table + " a, " + Role.TableName + " b," + TypeRole.TableName + " c," + Account.TableName + " d";
                query += " where a.roleid=b.id and a.userid=d.id and b.typeroleid=c.id";

                DataTable dt = DB.ExecuteDataTable(query, null);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public RoleUser GetBy(DataRow dr)
        {
            try
            {
                RoleUser obj = new RoleUser();
                obj.Id = (dr[RoleUser.COLUMN_ID] is DBNull) ? 0 : int.Parse(dr[RoleUser.COLUMN_ID].ToString());
                obj.RoleId = (dr[RoleUser.COLUMN_ROLEID] is DBNull) ? 0 : int.Parse(dr[RoleUser.COLUMN_ROLEID].ToString());
                obj.UserId = (dr[RoleUser.COLUMN_USERID] is DBNull) ? 0 : int.Parse(dr[RoleUser.COLUMN_USERID].ToString());

                return obj;
            }
            catch (Exception)
            {
            }

            return new RoleUser();
        }
        public RoleUser GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    return GetBy(dr);
                }
            }
            return new RoleUser();
        }
        public RoleUser GetByID(int Id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter(RoleUser.COLUMN_ID,Id),
										
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool haveRole(int userid, string PageName)
        {
            string query = "select * from " + table + " a," + Role.TableName + " b";
            query += " where a.roleid=b.id and a.userid=" + userid + " and b.pagename='" + PageName + "'";
            DataTable dt = DB.ExecuteDataTable(query, null);
            if (dt.Rows.Count > 0)
                return true;
            return false;
        }
        public DataTable Search(string rolename, int userid)
        {
            try
            {
                string query = "select a.*,b.Name as rolename, d.Username,c.Name as typerole from " + table + " a, " + Role.TableName + " b," + TypeRole.TableName + " c," + Account.TableName + " d";
                query += " where a.roleid=b.id and a.userid=d.id and b.typeroleid=c.id";
                if (userid > 0)
                {
                    query += string.Format(" and a.userid={0}", userid);
                }
                if (!string.IsNullOrEmpty(rolename))
                {
                    query += string.Format(" and b.PageName like '%{0}%'");
                }
                query += " order by rolename";
                DataTable dt = DB.ExecuteDataTable(query, null);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
